package cn.donting.jme.expansion.file;

import cn.donting.jme.EditorContent;
import cn.donting.jme.editor.scene.Scene;
import cn.donting.jme.event.file.OpenSceneEvent;
import cn.donting.jme.ioc.Bean;
import cn.donting.jme.swing.tab.scene.SceneTreeTab;
import cn.donting.jme.swing.tab.SceneViewTab;
import cn.donting.jme.utile.SaveUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

@Bean
@Slf4j
public class SceneOpenFile implements OpenFile {
    @Override
    public boolean isOpen(File file, String extName) {
        return extName.equals("scene");
    }

    @Override
    public void open(File file) {
        SceneTreeTab sceneTreeTab = EditorContent.getEditorUI().getTabOfAdd(SceneTreeTab.TAB_INDEX, SceneTreeTab.class);
        SceneViewTab sceneViewTab = EditorContent.getEditorUI().getTabOfAdd(SceneViewTab.TAB_INDEX, SceneViewTab.class);


        sceneTreeTab.setLoading();
        EditorContent.submitTask(() -> {
            try {
                Scene scene = SaveUtil.loadScene(file);
                OpenSceneEvent openSceneEvent = new OpenSceneEvent(file, scene);
                EditorContent.postEvent(openSceneEvent);
            } catch (IOException e) {
                log.error(e.getMessage(),e);
            }
        });
    }
}
